package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.App;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.customview.PhoneEditText;
import com.mmlc.network.filter.GetVCodeRequestFilter;
import com.mmlc.network.filter.RegisterAccountRequestFilter;
import com.mmlc.network.responsebean.GetVCodeResponseBean;
import com.mmlc.network.responsebean.LoginResponseBean;
import com.mmlc.network.responsebean.RegisterAccounResponseBean;
import com.mmlc.utils.ActivityManager;
import com.mmlc.utils.EditInputUtils;
import com.mmlc.utils.TimeCountUtil;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.UIUtils;
import com.mmlc.utils.ViewUtils;

/**
 * Created by Administrator on 2016/7/19 0019.
 */
public class RegisterAccount extends InterActivity implements View.OnClickListener {

    @Override
    protected int initContentView() {
        return R.layout.activity_registeraccount;
    }

    Button bt_getVCode;
    PhoneEditText et_phoneNumber;
    Button bt_register;
    EditText et_password;
    EditText et_vCode;
    ViewGroup rootView;
    TextView tv_title;
    ImageView iv_back;
    @Override
    protected void initView() {
        bt_getVCode = (Button) findViewById(R.id.bt_getVCode);
        et_phoneNumber = (PhoneEditText) findViewById(R.id.et_phoneNumber);
        bt_register = (Button) findViewById(R.id.bt_register);
        et_password = (EditText) findViewById(R.id.et_password);
        et_vCode = (EditText) findViewById(R.id.et_vCode);
        rootView = (ViewGroup) findViewById(R.id.rootView);
        tv_title = (TextView) findViewById(R.id.tv_title);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        ViewUtils.getView(activity, R.id.rl_register_extra).setVisibility(View.VISIBLE);
        UIUtils.updateTextSize(et_phoneNumber,"请输入手机号");
        UIUtils.updateTextSize(et_password,"请设置密码");
        UIUtils.updateTextSize(et_vCode,"请输入验证码");
        processData();
    }

    @Override
    protected void initEvent() {
    }

    private void processData() {
        //标题栏
        tv_title.setText("注册");
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //发送验证码
        bt_getVCode.setOnClickListener(this);
        //注册
        bt_register.setOnClickListener(this);
        ViewUtils.getView(activity, R.id.tv_hold).setOnClickListener(this);
        ViewUtils.getView(activity, R.id.tv_forgetPassword).setOnClickListener(this);
    }

    private void sendVCode() {
        //校验电话号码是否合法
        boolean isPhone = EditInputUtils.checkedPhoneNumber(et_phoneNumber.getText().toString());
        if (!isPhone) {
            ToastUtils.showToast(this, "电话号码不正确");
            return;
        }

        TimeCountUtil timeCountUtil = new TimeCountUtil(RegisterAccount.this, 60000, 1000,bt_getVCode);
        timeCountUtil.start();
        GetVCodeRequestFilter filter = new GetVCodeRequestFilter(this);
        filter.requestBean.phone = et_phoneNumber.getInputText();
        filter.requestBean.method = "sendVcode";
        filter.requestBean.type = 1;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<GetVCodeResponseBean>() {

            @Override
            public void onSuccess(GetVCodeResponseBean result) {
                ToastUtils.showToast(getApplicationContext(), "验证码发送成功");
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess);
            }
        });
    }

    private void register() {
        //检验手机号
        boolean isPhone = EditInputUtils.checkedPhoneNumber(et_phoneNumber.getText().toString());
        if (!isPhone) {
            ToastUtils.showToast(getApplicationContext(), "手机号不正确");
            return;
        }
        TimeCountUtil timeCountUtil = new TimeCountUtil(RegisterAccount.this, 60000, 1000, bt_getVCode);
        timeCountUtil.start();
        //校验密码
        boolean isName = EditInputUtils.checkPassword(getApplicationContext(),et_password.getText().toString());
        if(!isName){
            return;
        }
        //检验验证码
        boolean isVcode = EditInputUtils.checkedVcode(et_vCode);
        if (!isVcode) {
            ToastUtils.showToast(getApplicationContext(), "验证码不正确，请在短信中核实您的验证码");
            return;
        }
        RegisterAccountRequestFilter filter = new RegisterAccountRequestFilter(this);
        filter.requestBean.method = "sign";
        filter.requestBean.phone = et_phoneNumber.getInputText();
        filter.requestBean.password = et_password.getText().toString();
        filter.requestBean.vcode = et_vCode.getText().toString();
        filter.offerErrorParams(rootView);
        filter.isTransparence = true;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<RegisterAccounResponseBean>() {

            @Override
            public void onSuccess(RegisterAccounResponseBean result) {
                if (result.code == 0) {
                    ToastUtils.showToast(getApplicationContext(), "注册成功");
                }
                //保存用户信息
                String phone = et_phoneNumber.getText().toString();
                String password = et_password.getText().toString();
                App.user.saveUserInfo(phone,password,null);

                //自动进行登陆请求
                App.user.requestLogin(phone, password, RegisterAccount.this, new BaseRequestFilterLayer.AccessResultFromFilter<LoginResponseBean>() {
                    @Override
                    public void onSuccess(LoginResponseBean result) {
                        App.user.setLoginState(true);
                        ActivityManager.finishActivity(LoginActivity.class);
                        finish();
                    }

                    @Override
                    public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                        App.user.setLoginState(false);
                        finish();
                    }
                });
                /** TODO 虚拟登录
                 * 登录成功
                 * ActivityManager.finishActivity(LoginActivity.class);
                 * finish(); */
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                //停留在当前界面
                ToastUtils.showToast(serverError.mess);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_getVCode:
                sendVCode();
                break;
            case R.id.bt_register:
                UIUtils.hideKeyBoard(this,et_phoneNumber);
                UIUtils.hideKeyBoard(this,et_password);
                UIUtils.hideKeyBoard(this,et_vCode);
                register();
                break;
            case R.id.tv_hold:
                Intent intent1 = new Intent(this, LoginActivity.class);
                startActivity(intent1);
                finish();
                break;
            case R.id.tv_forgetPassword:
                Intent intent = new Intent(activity, ForgetPasswordActivity.class);
                startActivity(intent);
                break;
        }
    }


}
